// ClickedPints.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE = `attribute vec4 a_Position;
  attribute vec2 a_TexCoord;
  varying vec2 v_TexCoord;
  void main() {
    gl_Position =  a_Position;
    v_TexCoord = a_TexCoord;
  }`;

// Fragment shader program
var FSHADER_SOURCE = `precision mediump float;
  uniform sampler2D u_Sampler;
  varying vec2 v_TexCoord;
  uniform vec4 u_TexColor;
  void main() {
    gl_FragColor =  texture2D(u_Sampler,v_TexCoord) * u_TexColor;
  }`;

var angle = 0;
var u_TexColor;
var gl;

var r= 255;
var g=0;
var b=0;

var _r = 0;
var _g = 0;
var _b = 0;

function main() {
  // Retrieve <canvas> element
  var canvas = document.getElementById("webgl");

  // Get the rendering context for WebGL
  gl = getWebGLContext(canvas);
  if (!gl) {
    console.log("Failed to get the rendering context for WebGL");
    return;
  }

  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log("Failed to intialize shaders.");
    return;
  }

  //坐标
  var verticesPosition = new Float32Array([
    -0.5,0.5,
    -0.5,-0.5,
    0.5,0.5,
    0.5,-0.5,
  ]);

  var verticesPositionBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, verticesPositionBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, verticesPosition, gl.STATIC_DRAW);

  // // Get the storage location of a_Position
  var a_Position = gl.getAttribLocation(gl.program, "a_Position");
  if (a_Position < 0) {
    console.log("Failed to get the storage location of a_Position");
    return;
  }
  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(a_Position);


  //贴图坐标
  var verticesTexCoordsBuffer = gl.createBuffer();
  a_TexCoord = gl.getAttribLocation(gl.program, "a_TexCoord");
  if (a_TexCoord < 0) {
    console.log("Failed to get the storage location of a_TexCoord");
    return;
  }
  gl.enableVertexAttribArray(a_TexCoord);

  var verticesTexCoords = new Float32Array([
    0, 1,
    0, 0,
    1, 1,
    1, 0,
  ]);
  gl.bindBuffer(gl.ARRAY_BUFFER, verticesTexCoordsBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);
  gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, 0, 0);


  //获取片元着色器的u_TexColor地址
  u_TexColor = gl.getUniformLocation(gl.program, "u_TexColor");
  if (!u_TexColor) {
    console.log("Failed to get the storage location of u_TexColor");
    return;
  }


  // Specify the color for clearing <canvas>
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);

  //加载图片
  var texture = gl.createTexture();
  var u_Sampler = gl.getUniformLocation(gl.program, "u_Sampler");
  var image = new Image();
  image.onload = () => {
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
    gl.uniform1i(u_Sampler, 0);
    this.changeTexColor();
  };
  image.src = "resources/sky.jpg";
}

function changeTexColor(){
  if(r>=255&&g<=0){
    _r = -1;
    _g = 1;
    _b = 0;
  }else if(g>=255&&b<=0){
    _r = 0;
    _g = -1;
    _b = 1;
  }else if(b>=255&&r<=0){
      _r = 1;
      _g = 0;
      _b = -1;
  }
  r += _r;
  g += _g;
  b += _b;

  gl.uniform4f(u_TexColor,r/255,g/255,b/255,1);

  gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  requestAnimationFrame(() => {
    changeTexColor();
  });
}
